package optiondialoges;

import javax.swing.JPanel;
import java.awt.Frame;
import java.awt.BorderLayout;
import javax.swing.JDialog;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import javax.swing.JTextField;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.Serializable;

import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.JCheckBox;

import panel.GlyphPanel;


public class RotateOptionDialog extends JDialog implements ActionListener,WindowListener,Serializable {

    private static final long serialVersionUID = 1L;

    private JPanel jContentPane = null;

    private JPanel buttonPanel = null;

    private JPanel optionPanel = null;

    private JLabel angleLabel = null;

    private JTextField angleTextField = null;

    private JLabel interpolationLabel = null;

    private JComboBox interpolationComboBox = null;

    private JButton okButton = null;

    private JButton cancelButton = null;

    private JCheckBox fillWithBackgCheckBox = null;

    private JCheckBox enlargeCheckBox = null;

    private String angle;

    private String interpolation;

    private boolean fillWithBackg;

    private boolean enlarge;

    private GlyphPanel panel;

    /**
     * @param owner
     */
    public RotateOptionDialog(Frame owner,GlyphPanel panel) {
        super(owner);
        this.panel=panel;
        this.addWindowListener(this);
        initialize();
    }

    /**
     * This method initializes this
     * 
     * @return void
     */
    private void initialize() {
        this.setSize(287, 257);
        this.setContentPane(getJContentPane());
    }

    /**
     * This method initializes jContentPane
     * 
     * @return javax.swing.JPanel
     */
    private JPanel getJContentPane() {
        if (jContentPane == null) {
            jContentPane = new JPanel();
            jContentPane.setLayout(new BorderLayout());
            jContentPane.add(getButtonPanel(), BorderLayout.SOUTH);
            jContentPane.add(getOptionPanel(), BorderLayout.CENTER);
        }
        return jContentPane;
    }

    /**
     * This method initializes buttonPanel	
     * 	
     * @return javax.swing.JPanel	
     */
    private JPanel getButtonPanel() {
        if (buttonPanel == null) {
            GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
            gridBagConstraints5.gridx = 1;
            gridBagConstraints5.insets = new Insets(0, 10, 10, 10);
            gridBagConstraints5.gridy = 0;
            GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
            gridBagConstraints1.insets = new Insets(0, 10, 10, 10);
            buttonPanel = new JPanel();
            buttonPanel.setLayout(new GridBagLayout());
            buttonPanel.add(getOkButton(), gridBagConstraints1);
            buttonPanel.add(getCancelButton(), gridBagConstraints5);
        }
        return buttonPanel;
    }

    /**
     * This method initializes optionPanel	
     * 	
     * @return javax.swing.JPanel	
     */
    private JPanel getOptionPanel() {
        if (optionPanel == null) {
            GridBagConstraints gridBagConstraints6 = new GridBagConstraints();
            gridBagConstraints6.gridx = 1;
            gridBagConstraints6.anchor = GridBagConstraints.WEST;
            gridBagConstraints6.insets = new Insets(10, 0, 0, 0);
            gridBagConstraints6.gridy = 3;
            GridBagConstraints gridBagConstraints51 = new GridBagConstraints();
            gridBagConstraints51.gridx = 1;
            gridBagConstraints51.anchor = GridBagConstraints.WEST;
            gridBagConstraints51.insets = new Insets(10, 0, 0, 0);
            gridBagConstraints51.gridy = 2;
            GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
            gridBagConstraints4.fill = GridBagConstraints.VERTICAL;
            gridBagConstraints4.gridy = 1;
            gridBagConstraints4.weightx = 1.0;
            gridBagConstraints4.anchor = GridBagConstraints.WEST;
            gridBagConstraints4.insets = new Insets(10, 0, 0, 0);
            gridBagConstraints4.gridx = 1;
            GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
            gridBagConstraints3.gridx = 0;
            gridBagConstraints3.insets = new Insets(10, 10, 0, 10);
            gridBagConstraints3.anchor = GridBagConstraints.EAST;
            gridBagConstraints3.gridy = 1;
            interpolationLabel = new JLabel();
            interpolationLabel.setText("Interpolation");
            GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
            gridBagConstraints2.fill = GridBagConstraints.VERTICAL;
            gridBagConstraints2.gridy = 0;
            gridBagConstraints2.weightx = 1.0;
            gridBagConstraints2.insets = new Insets(10, 0, 0, 0);
            gridBagConstraints2.anchor = GridBagConstraints.WEST;
            gridBagConstraints2.gridx = 1;
            GridBagConstraints gridBagConstraints = new GridBagConstraints();
            gridBagConstraints.gridx = 0;
            gridBagConstraints.anchor = GridBagConstraints.EAST;
            gridBagConstraints.insets = new Insets(10, 10, 0, 10);
            gridBagConstraints.gridy = 0;
            angleLabel = new JLabel();
            angleLabel.setText("Angle");
            optionPanel = new JPanel();
            optionPanel.setLayout(new GridBagLayout());
            optionPanel.add(angleLabel, gridBagConstraints);
            optionPanel.add(getAngleTextField(), gridBagConstraints2);
            optionPanel.add(interpolationLabel, gridBagConstraints3);
            optionPanel.add(getInterpolationComboBox(), gridBagConstraints4);
            optionPanel.add(getFillWithBackgCheckBox(), gridBagConstraints51);
            optionPanel.add(getEnlargeCheckBox(), gridBagConstraints6);
        }
        return optionPanel;
    }

    /**
     * This method initializes angleTextField	
     * 	
     * @return javax.swing.JTextField	
     */
    private JTextField getAngleTextField() {
        if (angleTextField == null) {
            angleTextField = new JTextField();
            angleTextField.setPreferredSize(new Dimension(120, 21));
            angleTextField.setText("15.0");
        }
        return angleTextField;
    }

    /**
     * This method initializes interpolationComboBox	
     * 	
     * @return javax.swing.JComboBox	
     */
    private JComboBox getInterpolationComboBox() {
        if (interpolationComboBox == null) {
            String[] labels = { "None", "Bilinear", "Bicubic" };
            interpolationComboBox = new JComboBox(labels);
            interpolationComboBox.setPreferredSize(new Dimension(120, 26));
            interpolationComboBox.setSelectedIndex(0);
        }
        return interpolationComboBox;
    }

    /**
     * This method initializes okButton	
     * 	
     * @return javax.swing.JButton	
     */
    private JButton getOkButton() {
        if (okButton == null) {
            okButton = new JButton();
            okButton.setText("Ok");
            okButton.addActionListener(this);            
        }
        return okButton;
    }

    /**
     * This method initializes cancelButton	
     * 	
     * @return javax.swing.JButton	
     */
    private JButton getCancelButton() {
        if (cancelButton == null) {
            cancelButton = new JButton();
            cancelButton.setText("Cancel");
            cancelButton.addActionListener(this);
        }
        return cancelButton;
    }

    /**
     * This method initializes fillWithBackgCheckBox	
     * 	
     * @return javax.swing.JCheckBox	
     */
    private JCheckBox getFillWithBackgCheckBox() {
        if (fillWithBackgCheckBox == null) {
            fillWithBackgCheckBox = new JCheckBox();
            fillWithBackgCheckBox.setText("Fill With Background");
        }
        return fillWithBackgCheckBox;
    }

    /**
     * This method initializes enlargeCheckBox	
     * 	
     * @return javax.swing.JCheckBox	
     */
    private JCheckBox getEnlargeCheckBox() {
        if (enlargeCheckBox == null) {
            enlargeCheckBox = new JCheckBox();
            enlargeCheckBox.setText("Enlarge Image ");
        }
        return enlargeCheckBox;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        JButton button=(JButton)e.getSource();
        if(button.getText().contentEquals("Ok"))
        {
            angle=angleTextField.getText();
            interpolation=(String)interpolationComboBox.getSelectedItem();
            fillWithBackg=fillWithBackgCheckBox.isSelected();
            enlarge=enlargeCheckBox.isSelected();
            String command=String.format("run(\"Arbitrarily...\", \"angle=%s grid=0 interpolation=%s", angle, interpolation);
            if(fillWithBackg)command=String.format("%s fill", command);
            if(enlarge)command=String.format("%s enlarge", command);
            command=String.format("%s\");", command);
            panel.setOptionsSet(true);
            panel.setCommand(command);
            panel.setDialog(this);
        }
        else if (button.getText().contentEquals("Cancel")) {
            angleTextField.setText(angle);
            interpolationComboBox.setSelectedItem(interpolation);
            fillWithBackgCheckBox.setSelected(fillWithBackg);
            enlargeCheckBox.setSelected(enlarge);
        }
        this.setVisible(false);
    }

    @Override
    public void windowActivated(WindowEvent e) {
        // ignore
        
    }

    @Override
    public void windowClosed(WindowEvent e) {
        // ignore
        
    }

    @Override
    public void windowClosing(WindowEvent e) {
        // ignore
        
    }

    @Override
    public void windowDeactivated(WindowEvent e) {
        angleTextField.setText(angle);
        interpolationComboBox.setSelectedItem(interpolation);
        fillWithBackgCheckBox.setSelected(fillWithBackg);
        enlargeCheckBox.setSelected(enlarge);
        
    }

    @Override
    public void windowDeiconified(WindowEvent e) {
        // ignore
        
    }

    @Override
    public void windowIconified(WindowEvent e) {
        // ignore
        
    }

    @Override
    public void windowOpened(WindowEvent e) {
        angle=angleTextField.getText();
        interpolation=(String)interpolationComboBox.getSelectedItem();
        fillWithBackg=fillWithBackgCheckBox.isSelected();
        enlarge=enlargeCheckBox.isSelected();
        
    }

}  //  @jve:decl-index=0:visual-constraint="10,10"
